package wangjg.xuexiban.activity;

import java.io.File;

import wangjg.xuexiban.util.FileDownCallback;
import wangjg.xuexiban.util.FileDownCallback.DownloadVo;
import wangjg.xuexiban.util.WebLoader;
import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

public class BasicBizActivity extends BasicActivity {
	
	@SuppressLint("HandlerLeak")
	protected final Handler handler = new Handler(){
        @Override  
        public synchronized void handleMessage(Message msg) {
        	BasicBizActivity.this.handleMessage(msg);
        }
    };
    
    protected void sendMessage(int what){
    	sendMessage(what, null, 0);
    }
    
    protected void sendMessage(int what, Object obj, long delayMillis){
		Message message = handler.obtainMessage(what, obj);
		if(delayMillis<=0){
			handler.sendMessage(message);
		}else{
			handler.sendMessageDelayed(message, delayMillis);
		}
    }
    
    protected void handleMessage(Message msg) {
    	DownloadVo dv = (DownloadVo)msg.obj;
    	String filePath = dv.filePath;
    	int type = msg.what;
    	
    	File file = new File(filePath);
    	boolean fileExists = file.exists() && file.isFile();
    	
    	if(!fileExists && WebLoader.Callback.STATUS_FAILURE.equals(dv.status)){
    		Toast.makeText(BasicBizActivity.this, "加载网路"+FileDownCallback.getTypeName(type)+"失败!", Toast.LENGTH_SHORT).show();
    		return;
    	}
    	
		if (fileExists) {
			BasicBizActivity.this.handleDownload(type, dv);
		}
    }
    
    protected void download(String url, int type, View itemView){
		FileDownCallback ic1 = new FileDownCallback(this.handler, type, itemView);
		WebLoader.instance().loadFile(url, ic1);
    }

    protected void handleDownload(int type, DownloadVo dv) {
    	Log.d("handleDownload", dv.filePath);
		if(type==FileDownCallback.TYPE_AUDIO){
			this.handleAudio(dv);
		}else if(type==FileDownCallback.TYPE_IMG){
			this.handleImage(dv);
		}else if(type==FileDownCallback.TYPE_DATA){
    		this.handleData(dv);
		}
	}

    protected void handleData(DownloadVo dv) {
		
	}
    
    protected void handleAudio(DownloadVo dv){
    	
    }

    protected void handleImage(DownloadVo dv){
    	
    }
}
